package com.sayi.dzapi.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.MediaType;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;


import org.springframework.stereotype.Service;


import com.sayi.dzapi.entities.User;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class UserService {
    private static final Logger log = LoggerFactory.getLogger(UserService.class);

    // 从数据库中查询用户并验证密码
    public User authenticate(String username, String password) {
        // API URL
        String apiUrl = "https://verify.lty.fan"; // 替换为实际的 API 地址

        // 构建请求体
        Map<String, String> requestBody = new HashMap<>();
        requestBody.put("username", username);
        requestBody.put("password", password);

        // 创建 RestTemplate 实例
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 构建请求
        HttpEntity<Map<String, String>> request = new HttpEntity<>(requestBody, headers);

        try {
            // 发送 POST 请求
            ResponseEntity<Map> response = restTemplate.postForEntity(apiUrl, request, Map.class);

            // 解析响应
            if (response.getStatusCode() == HttpStatus.OK) {
                Map<String, Object> responseBody = response.getBody();
                if (responseBody != null ) {
                    log.info(responseBody.toString());

                    

                    int code=(int) responseBody.get("code");
                    String message=(String)responseBody.get("message");
                    switch(code){
                        case 200:
                            // 验证成功，返回用户对象
                            Map<String, Object> userMap = (Map<String, Object>) responseBody.get("user");
                            
                            if (userMap != null) {
                                
                                // 创建用户对象并填充信息
                                int uid=Integer.parseInt((String) userMap.get("uid"));
                                User user = new User(uid,(String) userMap.get("username"),(String)userMap.get("email"));
                                return user;
                            }
                            break;
                        default:
                            User user = new User(code,message);
                            return user;
                    }
                }
            }
        } catch (HttpClientErrorException | HttpServerErrorException e) {
            // 捕获 HTTP 错误
            System.err.println("Error during API call: " + e.getMessage());
        } catch (Exception e) {
            // 捕获其他错误
            System.err.println("Unexpected error: " + e.getMessage());
        }

        // 验证失败，返回 null
        return null;
    }

}
